﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System.Reflection;
using System.Linq;
using System;

[CustomEditor(typeof(CharacterInfo))]
public class CharacterInfoEditor : Editor
{
    SerializedProperty hpSP;
    SerializedProperty mpSP;
    SerializedProperty characterNameSP;
    SerializedProperty attacksSP;
    SerializedProperty idSP;
    SerializedProperty animtorControllerSP;
    CharacterInfo character;

    ReorderableList attacksRL;


    private void OnEnable()
    {

        character = target as CharacterInfo;
        characterNameSP = serializedObject.FindProperty("characterName");
        hpSP = serializedObject.FindProperty("maxHp");
        mpSP = serializedObject.FindProperty("maxMp");
        idSP = serializedObject.FindProperty("id");
        
        attacksSP = serializedObject.FindProperty("attacks");
        animtorControllerSP = serializedObject.FindProperty("animatorController");
        //attacksRL = new ReorderableList(serializedObject, attacksSP, true, true, true, true);
        //attacksRL.drawHeaderCallback = (Rect rect) =>
        //  {
        //      GUI.Label(rect, "攻击方式");
        //  };
        //attacksRL.drawElementCallback = (Rect rect, int index, bool selected, bool focused) =>
        //  {
        //      SerializedProperty item = attacksRL.serializedProperty.GetArrayElementAtIndex(index);
        //      rect.height = EditorGUIUtility.singleLineHeight;
        //      rect.y += 2;
        //      EditorGUI.BeginDisabledGroup(true);
        //      EditorGUI.PropertyField(rect, item, new GUIContent("攻击 " + index));
        //      EditorGUI.EndDisabledGroup();
        //  };
        //attacksRL.onAddDropdownCallback = (Rect rect, ReorderableList list) =>
        //{
        //    //Debug.Log("onAddDropdownCallback");
        //    GenericMenu menu = new GenericMenu();

        //    var allAttackType = AssetDatabase.FindAssets("t:Script", new string[] { "Assets/Scripts/Character/Attack" });

        //    foreach (var t in allAttackType)
        //    {
        //        string path = AssetDatabase.GUIDToAssetPath(t);
        //        string[] s = path.Split('/');
        //        string fileName = s[s.Length - 1];
        //        menu.AddItem(new GUIContent(fileName), false, ClickHander,fileName);
        //    }
        //    menu.ShowAsContext();
        //};
    }

    private void ClickHander(object target)
    {
        string fileName = ((string)target).Split('.')[0];
        //Debug.Log(fileName);
        if (attacksRL.serializedProperty != null)
        {
            attacksRL.serializedProperty.arraySize++;
            attacksRL.index = attacksRL.serializedProperty.arraySize - 1;
            SerializedProperty item = attacksRL.serializedProperty.GetArrayElementAtIndex(attacksRL.index);
            item.stringValue = fileName;
        }
        else {
            ReorderableList.defaultBehaviours.DoAddButton(attacksRL);
        }
        serializedObject.ApplyModifiedProperties();
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUILayout.PropertyField(idSP, new GUIContent("角色ID"));

        EditorGUILayout.PropertyField(characterNameSP, new GUIContent("角色名称"));
        EditorGUILayout.PropertyField(hpSP, new GUIContent("最大血量"));
        EditorGUILayout.PropertyField(mpSP, new GUIContent("最大蓝量"));
        EditorGUILayout.PropertyField(animtorControllerSP, new GUIContent("动作控制器"));
        EditorGUILayout.PropertyField(attacksSP, new GUIContent("攻击配置"));
        //attacksRL.DoLayoutList();

        serializedObject.ApplyModifiedProperties();
    }
}


